package com.xiaoshuidi.cloud.module.member.controller.admin.coupon;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.excel.core.util.ExcelUtils;
import com.xiaoshuidi.cloud.module.member.controller.admin.coupon.vo.*;
import com.xiaoshuidi.cloud.module.member.controller.app.coupon.vo.CouponCustomerValidQueryVo;
import com.xiaoshuidi.cloud.module.member.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.member.service.coupon.CouponService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
/**
 * @version generator
 * @description 优惠券表
 * @date 2023-03-14 15:06:11
 */
@Tag(name = "管理后台 - 优惠券管理")
@RequestMapping(ApiConstants.NAME + "/coupon")
@RestController
@Slf4j
public class AdminCouponController {

    @Autowired
    CouponService couponService;


    @PostMapping("/save")
    @Operation(summary = "新增优惠券")
    @PreAuthorize("@ss.hasAnyPermissions('member:coupon:create')")
    public CommonResult<Long> saveCoupon(@RequestBody @Valid CouponForm form) {
        Long id = couponService.saveCoupon(form);
        return CommonResult.success(id);
    }

    @PostMapping("/update")
    @Operation(summary = "修改优惠券")
    public CommonResult<Boolean> updateCoupon(@RequestBody CouponUpdateForm form) {
        couponService.updateCoupon(form);
        return CommonResult.success(Boolean.TRUE);
    }

    @PostMapping("/to-approval/{id}")
    @Operation(summary = "提交审核")
    public CommonResult<Boolean> toApproval(@PathVariable("id") Long id) {
        return CommonResult.success(couponService.toApproval(id));
    }

    @GetMapping("/get")
    @Operation(summary = "优惠券详情")
    public CommonResult<CouponDetailsVo> queryCouponDetailed(@RequestParam("id") Long couponId) {
        return CommonResult.success(couponService.queryCouponDetailed(couponId));
    }

    @PostMapping("/page")
    @Operation(summary = "优惠券活动管理--查询优惠券")
    public CommonResult<PageResult<CouponListVo>> queryCoupon(@RequestBody CouponQueryForm form) {
        PageResult<CouponListVo> couponVo = couponService.queryCoupon(form);
        return CommonResult.success(couponVo);
    }

    @GetMapping("/query-valid-coupon")
    @Operation(summary = "查询有效的优惠券")
    public CommonResult<List<CouponCustomerValidQueryVo>> queryValidCoupon() {
        List<CouponCustomerValidQueryVo> couponVo = couponService.queryValidCoupon(Lists.newArrayList(3, 4));
        return CommonResult.success(couponVo);
    }

    @PostMapping("/query-approve-coupon")
    @Operation(summary = "审批列表-查询优惠券")
    public CommonResult<CouponPageVo> queryApproveCoupon(@RequestBody CouponQueryForm form) {
        PageResult<CouponListVo> couponVos = couponService.queryApproveCoupon(form);
        CouponPageVo couponPageVo = couponService.couponSummaryData();
        couponPageVo.setPageData(couponVos);
        return CommonResult.success(couponPageVo);
    }

    @PostMapping("/coupon-send-detail")
    @Operation(summary = "优惠明细")
    public CommonResult<PageResult<CouponDetailedVo>> couponSendDetail(@RequestBody CouponDetailedForm form) {
        PageResult<CouponDetailedVo> pageInfo = couponService.detailed(form);
        return CommonResult.success(pageInfo);
    }

    @PostMapping("/delete")
    @PreAuthorize("@ss.hasAnyPermissions('member:coupon:delete')")
    @Operation(summary = "删除优惠活动")
    public CommonResult<Boolean> delete(@Validated @RequestBody CouponOperateForm form) {
        couponService.delete(form.getCouponId());
        return CommonResult.success(Boolean.TRUE);
    }

    @PostMapping("/stop")
    @Operation(summary = "终止优惠活动")
    public CommonResult<Boolean> stop(@Validated @RequestBody CouponOperateForm form) {
        couponService.stop(form.getCouponId());
        return CommonResult.success(Boolean.TRUE);
    }

    @PostMapping("/start")
    @Operation(summary = "启动优惠活动")
    public CommonResult<Boolean> start(@Validated @RequestBody CouponOperateForm form) {
        couponService.start(form.getCouponId());
        return CommonResult.success(Boolean.TRUE);
    }

    @PostMapping("/export-coupon-list")
    @Operation(summary = "优惠券列表导出")
    public void exportCouponList(@RequestBody CouponQueryForm form, HttpServletResponse response) throws IOException {
        List<CouponListExportVO> list = couponService.exportCouponList(form);
        log.info("导出优惠券列表数据：{}", JSONObject.toJSONString(list));
        ExcelUtils.write(response, "优惠券列表.xls", "优惠券列表", CouponListExportVO.class, list);
    }

}

